package com.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.dao.UserAddressMapper;
import com.shop.pojo.*;
import com.shop.service.UserAddressService;
import com.shop.service.LoginService;
import com.shop.util.BaseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
public class UserAddressserviceimpl implements UserAddressService {
    @Resource
    private UserAddressMapper userAddressMapper;
    @Resource
    private LoginService loginService;

    /*用户地址数据查询*/
    @Override
    public List<UserAddress> queryUserOrderByCondition(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String data = request.getParameter("data");
        String token =  request.getParameter("token");
        User user = loginService.getUserByToken(token);     //验证用户信息是否已过期
        if(null == user){
            return null;
        }

        Map<String,Object> params = null;
        if(StringUtils.isNotEmpty(data)){
            params = BaseUtil.json2Map(data);
        }else{
            params = new HashMap<>();
        }

        params.put("userId",user.getUserId());

        List<UserAddress> userAddressList = userAddressMapper.queryUserOrderByCondition(params);
        return userAddressList;
    }

    /*用户地址添加*/
    @Override
    public String addUserAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String data = request.getParameter("data");
        String token =  request.getParameter("token");
        User user = loginService.getUserByToken(token);     //验证用户信息是否已过期
        if(null == user){
            return "添加失败：用户已失效，请重新登录后再试";
        }

        data = BaseUtil.dataTranscoding(data);
        String dataJson = BaseUtil.toJson(data);    //把数据转化为Json字符串
        UserAddress userAddress = JSONObject.parseObject(dataJson,UserAddress.class);
        userAddress.setId(BaseUtil.getSID());
        userAddress.setCreateTime(new Date());

        userAddressMapper.insert(userAddress);
        return "添加成功！";
    }

    /*用户地址更新*/
    @Override
    public String updateUserAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String data = request.getParameter("data");
        String token =  request.getParameter("token");
        User user = loginService.getUserByToken(token);     //验证用户信息是否已过期
        if(null == user){
            return "更新失败：用户已失效，请重新登录后再试";
        }

        data = BaseUtil.dataTranscoding(data);
        String dataJson = BaseUtil.toJson(data);    //把数据转化为Json字符串
        UserAddress userAddress = JSONObject.parseObject(dataJson,UserAddress.class);
        userAddress.setUpdateTime(new Date());

        userAddressMapper.updateByPrimaryKey(userAddress);
        return "更新成功！";
    }

    /*用户地址删除*/
    @Override
    public String deleteUserAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String addressId = request.getParameter("addressId");
        String token =  request.getParameter("token");
        User user = loginService.getUserByToken(token);     //验证用户信息是否已过期
        if(null == user){
            return "删除失败：用户已失效，请重新登录后再试";
        }

        userAddressMapper.deleteByPrimaryKey(addressId);
        return "删除成功！";
    }

    @Override
    public List<UserAddress> selectAll() {
        return userAddressMapper.queryData();
    }

}


